package com.lovly.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;

/**
 * Created by lovly on 2018/10/19.
 */
@Configuration
public class InitConfig {

    @Autowired
    private CodeProperties codeProperties;

    @PostConstruct
    public void init(){

        try {

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(codeProperties.getBasePath())));
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                if(!"".equals(str.trim())){
                    CacheBuffer.CODES.add(str);
                }

            }
            bufferedReader.close();

            /**
             * 根据目录获取图片文件
             */

            File fileBackground = new File( getClass().getResource(codeProperties.getBasePathBackground()).getPath() );
            File[] files = fileBackground.listFiles();
            for (File f : files) {
                String name = f.getName().toLowerCase();
                if (name.endsWith(".jpg") || name.endsWith(".png")) {
                    CacheBuffer.IMAGES.add(f.getAbsolutePath());
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("验证码读取失败");
        }

    }


}
